package com.yanqu.road.dao.impl.maidian;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.log.maidian.LogUserBagChange;
import com.yanqu.road.entity.maidian.UserMaiDianLoginData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserMaiDianLoginDataDaoImpl extends TempDataObjectDao<UserMaiDianLoginData> {


    public static String getUserMaiDianStateDataSql = "select * from `t_u_maidian_login`";
    public Map<Long, UserMaiDianLoginData> getUserMaiDianStateData() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, UserMaiDianLoginData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserMaiDianStateDataSql);
            rs = call.executeQuery();
            while (rs.next()) {
                UserMaiDianLoginData stateData = parse(rs);
                map.put(stateData.getUserId(), stateData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateUserMaiDianStateDataSql = "update `t_u_maidian_login` set loginTime=? where userId=?";
    @Override
    protected boolean updateData(UserMaiDianLoginData userMaiDianStateData) {
        userMaiDianStateData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(userMaiDianStateData.getLoginTime(), ",")));
        para.put(i++, new DbParameter(Types.BIGINT, userMaiDianStateData.getUserId()));
        if (getDb().executeNoneQuery(updateUserMaiDianStateDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserMaiDianStateDataSql = "insert into `t_u_maidian_login`(userId,loginTime) values (?,?)";
    @Override
    protected boolean addData(UserMaiDianLoginData userMaiDianStateData) {
        userMaiDianStateData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userMaiDianStateData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(userMaiDianStateData.getLoginTime(), ",")));
        if (getDb().executeNoneQuery(insertUserMaiDianStateDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserMaiDianLoginData parseData(ResultSet rs) throws SQLException {
        UserMaiDianLoginData userMaiDianStateData = new UserMaiDianLoginData();
        userMaiDianStateData.setUserId(rs.getLong("userId"));
        userMaiDianStateData.setLoginTime(StringUtils.stringToLongList(rs.getString("loginTime"), ","));

        return userMaiDianStateData;
    }

    /**
     * 背包道具
     */
    public Map<Integer, List<LogUserBagChange>> selectUserBagChange(long userId, String time){
        String sql = "SELECT way,isGet,goodsId,sum(num) as total,count(0) as times FROM t_l_goods_2  WHERE date = ?  and userId = ?  GROUP BY way,isGet,goodsId ";
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<LogUserBagChange>> map = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getLogDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setString(i++, time);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                LogUserBagChange bagChange = new LogUserBagChange();
                bagChange.setWay(rs.getInt("way"));
                bagChange.setIsGet(rs.getInt("isGet"));
                bagChange.setGoodsId(rs.getInt("goodsId"));
                bagChange.setTotal(rs.getLong("total"));
                bagChange.setTimes(rs.getInt("times"));
                if(!map.containsKey(bagChange.getIsGet())){
                    map.put(bagChange.getIsGet(), new ArrayList<>());
                }
                map.get(bagChange.getIsGet()).add(bagChange);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }


    /**
     * 元宝
     */
    public Map<Integer, List<LogUserBagChange>> selectUserIngotsChange(long userId, String startTime, String endTime){
        String sql = "SELECT   son as way,  isGet,  sum(`value`) as total,  count(0) as times  FROM  t_l_ingots  WHERE 1=1  and time >= ? and time < ?  and userId = ?  GROUP BY son,isGet;";
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<LogUserBagChange>> map = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getLogDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setString(i++, startTime);
            call.setString(i++, endTime);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                LogUserBagChange bagChange = new LogUserBagChange();
                bagChange.setWay(rs.getInt("way"));
                bagChange.setIsGet(rs.getInt("isGet"));
                bagChange.setTotal(rs.getLong("total"));
                bagChange.setTimes(rs.getInt("times"));
                if(!map.containsKey(bagChange.getIsGet())){
                    map.put(bagChange.getIsGet(), new ArrayList<>());
                }
                map.get(bagChange.getIsGet()).add(bagChange);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
}
